package com.umeox.watch.moto.launcher.utils;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.provider.CallLog;
import android.text.TextUtils;

import com.umeox.moto.common.utils.SystemUtils;
import com.umeox.watch.moto.dataservice.DataProvider;
import com.umeox.watch.moto.launcher.bean.NotificationBean;

public class MessageUtil {

    private static long sLastTimeCall;

    /**
     * 获取未接视频电话、VOIP电话记录
     */
    public static NotificationBean getMissedVideoCalled(Context context, String friendId) {
        NotificationBean missCallBean = null;
        final String phoneNum = DataProvider.ContactProvider.PHONE_NUM;
        final String nickName = DataProvider.ContactProvider.NICKNAME;
        final Cursor cursor = context.getContentResolver()
                .query(DataProvider.ContactProvider.getUri()
                        , null
                        , DataProvider.ContactProvider.FRIEND_ID + "=?"
                        , new String[]{friendId}, null);
        try {
            if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
                missCallBean = new NotificationBean();
                missCallBean.setId(cursor.getString(cursor.getColumnIndex(phoneNum)));
                missCallBean.setTitle(cursor.getString(cursor.getColumnIndex(nickName)));
                missCallBean.setType(NotificationBean.TYPE_CALL);

                ContentValues callLogs = DataProvider.getLastVideoCallLogs(friendId);
                if (callLogs != null) {
                    long duration = (long) callLogs.get(DataProvider.VideoCallProvider.CALL_DURATION);
                    boolean newCall = (int) callLogs.get(DataProvider.VideoCallProvider.NEW_CALL) == 1;
                    missCallBean.setCount(duration == 0 && newCall ? 1 : 0);
                    missCallBean.setTime((Long) callLogs.get(DataProvider.VideoCallProvider.END_TIME));
                }
            }
            return missCallBean;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }

    }


    /**
     * 获取未接电话信息
     *
     * @param context
     * @param refusePhoneState
     * @return
     */
    @SuppressLint("MissingPermission")
    public static NotificationBean getMissedCallBean(Context context, boolean refusePhoneState) {

        sLastTimeCall = 0;
        NotificationBean missCallBean = null;
        Cursor cursor = context.getContentResolver().query(
                CallLog.Calls.CONTENT_URI, new String[]{CallLog.Calls.NUMBER, CallLog.Calls.DATE},
                " type=? and new=?",
                new String[]{CallLog.Calls.MISSED_TYPE + "", "1"}, "date desc");

        if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
            missCallBean = new NotificationBean();
            missCallBean.setType(NotificationBean.TYPE_CALL);
            final String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
            String contactNameByPhoneNumber = SystemUtils.getContactNameByPhoneNumber(context, number);
            long date = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE));
            if (date <= sLastTimeCall) {
                return null;
            }
            if (TextUtils.isEmpty(contactNameByPhoneNumber)) {
                if (refusePhoneState) {
                    return null;
                }
                contactNameByPhoneNumber = number;
                missCallBean.setType(NotificationBean.TYPE_CALL_UNKNOWN);
            }
            sLastTimeCall = date;
            missCallBean.setTitle(contactNameByPhoneNumber);
            missCallBean.setTime(date);
            missCallBean.setId(number);
            missCallBean.setCount(1);
        }
        if (cursor != null) {
            cursor.close();
        }
        return missCallBean;
    }

    /**
     * 获取语聊未读消息数量
     *
     * @param context
     * @return
     */
    public static int getUnreadChatMessageCount(Context context) {
        int result = 0;
        Cursor cursor = null;

        try {
            cursor = context.getContentResolver().query(DataProvider.ChatContactProvider.getUnreadCountUri(), null, null, null, "");
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getInt(cursor.getColumnIndex("totalCount"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return result;
    }
}
